package hunterxhunter.greedisland.entities.sprite;

import hunterxhunter.greedisland.data.enums.Collision;
import static hunterxhunter.greedisland.data.enums.Collision.LD;
import java.awt.Dimension;
import java.awt.Point;
import framework.eea.entity.Entity;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class BodyEntity extends Entity {

    private Body body;
    
    public BodyEntity(String id, BodyType type, Point position, Dimension size, float friction, float density, World world) {
        super(id);
        
        //define body and create in world
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = type;
        bodyDef.position.set(position.x, position.y);
        body = world.createBody(bodyDef);
        
        //crate shape of body
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(size.width/2.0f, size.height/2.0f);
        shape.setRadius(Math.min(size.width, size.height)/4.0f);
        setSize(size);
        
        //create fixture
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.friction = friction;
        fixtureDef.density = density;
        body.createFixture(fixtureDef);
    }

    public BodyEntity(String id, Point position, Dimension size, Collision collision, World world) {
        super(id);
        
        //define body and create in world
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.STATIC;
        bodyDef.position.set(position.x, position.y);
        body = world.createBody(bodyDef);
        
        //crate shape of body
        PolygonShape shape = new PolygonShape();
        
        float w2 = size.width/2.0f;
        float h2 = size.height/2.0f;
        switch(collision) {
            case LD: shape.set(new Vec2[] { new Vec2(0,0), new Vec2(w2, h2), new Vec2(0, h2)  }, 3); break;
            case LU: shape.set(new Vec2[] { new Vec2(0,0), new Vec2(w2, 0), new Vec2(0, h2) }, 3); break;
            case RD: shape.set(new Vec2[] { new Vec2(0,h2), new Vec2(w2, h2), new Vec2(w2, 0) }, 3); break;
            case RU: shape.set(new Vec2[] { new Vec2(0,0), new Vec2(w2, 0), new Vec2(w2, h2) }, 3); break;
            case ALL: shape.setAsBox(w2, h2); break;
        }
        setSize(size);
        
        //create fixture
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.friction = 0;
        fixtureDef.density = 0;
        body.createFixture(fixtureDef);
    }
    
    @Override
    public Point getPosition() {
        Vec2 pos = body.getPosition();
        return new Point(Math.round(pos.x), Math.round(pos.y));
    }
    
    /**
     * @return the body
     */
    public Body getBody() {
        return body;
    }

    /**
     * @param body the body to set
     */
    public void setBody(Body body) {
        this.body = body;
    }
    
}
